package action.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import wepayu.utils.Data;

/**
 * Form para obter uma data.
 * @author Gustavo de Farias
 * @author Lucas Albertins
 */
public class DataForm extends ActionForm {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4518006868620582916L;
	
	protected String data;
	
	/**
	 * Obtem a propriedade do form.
	 * @return Returns the data.
	 */
	public String getData() {
		return data;
	}

	/**
	 * Altera a propriedade do form.
	 * @param data The data to set.
	 */
	public void setData(String data) {
		this.data = data;
	}

	/**
	 * Valida os valores presentes no form.
	 * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		
		ActionErrors erros = new ActionErrors();
		
		if (this.data == null || this.data.trim().equals("")) {
			erros.add("erroData", new ActionMessage("erro.cadastro.vazio", "Data", "a"));
		}
		else {
			try {
				new Data(this.data);
			}
			catch (Exception e) {
				erros.add("erroData", new ActionMessage("erro.cadastro.invalido", "Data", "a"));
			}
		}

		request.setAttribute("dataForm", this);
		
		return erros;
	}
}